package com.ecy.quadratic.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ecy.quadratic.R;
import com.ecy.quadratic.beans.ComicBroswer;
import com.yuyh.easyadapter.glide.GlideRoundTransform;

import java.util.List;

public class ComicBrowserAdapter extends BaseAdapter {

    private List<ComicBroswer> mComicList;
    private Context mContext;

    public ComicBrowserAdapter(Context context, List<ComicBroswer> comicList) {
        mContext = context;
        mComicList = comicList;
    }

    public void updateData(List<ComicBroswer> comicList) {
        mComicList.clear();
        mComicList.addAll(comicList);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mComicList == null ? 0 : mComicList.size();
    }

    @Override
    public ComicBroswer getItem(int position) {
        if (mComicList != null) {
            return mComicList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_comic_browser_item, null);
            viewHolder = new ViewHolder();
            viewHolder.comicTitleTV = (TextView) convertView.findViewById(R.id.comic_browser_title_tv);

            viewHolder.comicReadChapterTV = (TextView) convertView.findViewById(R.id.comic_browser_readChapter_tv);
            viewHolder.comicCoverIV = (ImageView) convertView.findViewById(R.id.comic_browser_cover_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ComicBroswer comicBrowser = getItem(position);
        if (comicBrowser.getAffiliatedComic() != null) {
            viewHolder.comicTitleTV.setText(comicBrowser.getAffiliatedComic().getTitle());
            String img = !TextUtils.isEmpty(comicBrowser.getAffiliatedComic().getImg()) ? comicBrowser.getAffiliatedComic().getImg() : "http://image.samanlehua.com/mh/" + comicBrowser.getAffiliatedComic().getId() + ".jpg";

            Glide.with(mContext).load(img).placeholder(R.drawable.cover_default).transform(new GlideRoundTransform(mContext)).into(viewHolder.comicCoverIV);
        }
        viewHolder.comicReadChapterTV.setText("阅读至" + comicBrowser.getReadingSectionBean().getChapter_name());
        return convertView;
    }

    public class ViewHolder {
        public TextView comicTitleTV;
        public TextView comicReadChapterTV;
        public ImageView comicCoverIV;
    }

}
